package com.xb.cloud.usercenter.service.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.xb.cloud.common.model.ExceptionLoggerDetail;
import com.xb.cloud.gateway.client.ReturnCodes;
import com.xb.cloud.gateway.client.ReturnUtils;

@Aspect
@Component("userCenterAdvice")
public class ApiServiceAdvice {

	private final Logger LOGGER = LoggerFactory.getLogger(ApiServiceAdvice.class);

	@Pointcut("execution (* com.xb.cloud.usercenter.service.*.*(..))")
	private void pointcut() {
	};

	@Around(value = "pointcut()")
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
		LOGGER.info("around");
		String className = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();
		try {
			Object retVal = joinPoint.proceed();
			return retVal;
		} catch (Throwable e) {
			ExceptionLoggerDetail loggerDetail = new ExceptionLoggerDetail(e, className, methodName, args);
			LOGGER.error(JSON.toJSONString(loggerDetail));
			// TODO:根据不同的exception类返回不同的错误码
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

}
